<template>

  <el-form ref="form" :model="form" label-width="120px" :rules="rules">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="form.username" autocomplete="off" style="width:50%" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="form.mobile" autocomplete="off" style="width:50%" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker v-model="form.timeOfEntry" type="date" placeholder="选择入职时间" style="width:50%" />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select v-model="form.formOfEmployment" placeholder="请选择" style="width:50%">
        <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id" />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="form.workNumber" autocomplete="off" style="width:50%" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input v-model="form.departmentName" autocomplete="off" style="width:50%" @focus="isOnFocus" />
      <el-tree v-if="isShow" :data="treeList" :props="{label:'name'}" @node-click="hNodeClick" />
    </el-form-item>
    <el-form-item label="转正时间">
      <el-date-picker v-model="form.timeOfEntry" type="date" placeholder="选择转正时间" style="width:50%" />
    </el-form-item>
    <el-form-item>
      <el-button @click="$emit('close')">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </el-form-item>
  </el-form>

</template>

<script>
// 获取部门列表
import { getDepartments } from '@/api/departments'
// 引入树状结构
import { tranListToTreeData } from '@/utils/index'
// 导入枚举文件
import EmployeeEnum from '@/constant/employees'
export default {
  name: '',
  components: {},
  props: {},
  data() {
    return {
      isShow: false,
      // 部门树形数据
      treeList: [],
      hireType: EmployeeEnum.hireType,
      form: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      }

    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    //   点击确定按钮
    submit() {
      this.$refs.form.validate(valide => {
        if (valide) {
          this.$emit('success', this.form)
        }
      })
    },
    //  点击选择部门框
    async isOnFocus() {
      const res = await getDepartments()
      //   去掉一第一个元素
      res.data.depts.shift()
      this.treeList = await tranListToTreeData(res.data.depts)
      this.isShow = true
    },
    // 点击部门父节点
    hNodeClick(data, node) {
    //   console.log(data, node)
    // 如果当前部门还有子部门,则不能被选中
      if (data.children.length) return
      // 把选中的值给当前表单
      this.form.departmentName = data.name
      // 隐藏树形结构
      this.isShow = false
    }
  }
}
</script>

<style scoped>

</style>
